<template>
  <div id='index'>
    <div class="card-container">
      <el-table :data="tableData" height="600" style="width: 100%">
        <el-table-column prop="type" align="center" show-overflow-tooltip label="类型" min-width="160">
          <template slot-scope="scope">
            <span>
              {{ scope.row.type == '1' ? '设备监控消息' : '速报消息' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="appToken" align="center" show-overflow-tooltip label="appToken" min-width="160">
        </el-table-column>
        <el-table-column prop="topicId" align="center" show-overflow-tooltip label="主题ID" min-width="80">
        </el-table-column>
        <el-table-column prop="platformName" align="center" show-overflow-tooltip label="平台名称" min-width="100">
        </el-table-column>
        <el-table-column prop="enable" align="center" show-overflow-tooltip label="是否启用" min-width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enable" :disabled="switchLoad" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" min-width="160">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEidt(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleTest(scope.row)">测试</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="编辑" :show-close="false" :close-on-click-modal="false" v-if="dialogVisible"
      :visible.sync="dialogVisible" width="45%">
      <div class="dialogBox">
        <div class="card-container">
          <el-row style="width: 100%" :gutter="10">
          <el-form ref="form" :model="form" class="form" :rules="rules" label-width="120px">
            <el-form-item label="类型" prop="type">
              <el-col :span="24" :offset="0">
                <el-input :value="form.type == '1' ? '预警消息' : '速报消息'" disabled />
              </el-col>
            </el-form-item>
            <el-form-item label="APP Token " prop="appToken">
              <el-col :span="24" :offset="0">
                <el-input v-model="form.appToken" placeholder="请输入APP Token" />
              </el-col>
            </el-form-item>
            <el-form-item label="主题ID" prop="topicId">
              <el-col :span="24" :offset="0">
                <el-input v-model="form.topicId" placeholder="请输入主题ID" />
              </el-col>
            </el-form-item>
            <el-form-item label="平台名称" prop="platformName">
              <el-col :span="24" :offset="0">
                <el-input v-model="form.platformName" placeholder="请输入平台名称" />
              </el-col>
            </el-form-item>
          </el-form>
        </el-row>
        </div>
       
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handlerCancel">取 消</el-button>
        <el-button  @click="handlerOn">
          提 交
        </el-button>
      </span>
    </el-dialog>

  </div>
</template>
  
<script>
import { wxmessageList, wxmessageId, putWxmessage, TestWxmessage } from '@/api/configuration/messagePush/index.js';
export default {
  name: 'messagePush',
  data() {
    return {
      dialogVisible: false,
      switchLoad: false,
      tableData: [],
      form: {},
      rules: {
        appToken: [{ required: true, message: "appToken不能为空", trigger: "blur" }],
        topicId: [{ required: true, message: "主题ID不能为空", trigger: "blur" }],
        platformName: [{ required: true, message: "平台名称不能为空", trigger: "blur" }],
      },
    };
  },
  computed: {},
  created() {
  },
  mounted() {
    this.getwxList();
  },
  beforeCreate() { },
  beforeMount() { },
  methods: {
    // 获取配置列表
    async getwxList() {
      const res = await wxmessageList()
      if (res.code == 200) {
        this.tableData = [res.data];
      }
    },
    // 是否启用switch开关
    async handleStatusChange(row) {
      // console.log(row,'row')
      this.switchLoad = true;
      let text = "";
      if (row.enable == "1") {
        text = "是否启用？";
      } else {
        text = "是否断开？";
      }
      this.$modal
        .confirm(text)
        .then(async () => {
          this.reviseMsg(row)
          this.$modal.msgSuccess(row.enable == "1" ? '启用成功' : '断开成功');
          this.switchLoad = false;
        })
        .catch(() => {
          this.switchLoad = false;
          this.getwxList()
        });
    },
    // 编辑按钮
    handleEidt(row) {
      this.getwxMsgId(row.id)
    },

    // 测试按钮
    async handleTest(row) {
      const res = await TestWxmessage(row.type)
      if (res.code == 200) {
        this.$modal.msgSuccess('测试发送成功');
      } else {
        this.$modal.msgError('测试发送失败');
      }
    },

    // 单条查询
    async getwxMsgId(id) {
      const res = await wxmessageId(id)
      if (res.code == 200) {
        this.form = res.data;
        this.dialogVisible = true
      }
    },
    // 修改配置
    async reviseMsg(data) {
      const res = await putWxmessage(data)
      if (res.code == 200) {
        this.dialogVisible = false
        this.getwxList()
      }
    },
    // 取消按钮
    handlerCancel() {
      this.dialogVisible = false;
      this.form = {};
    },
    // 提交按钮
    handlerOn() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.reviseMsg(this.form)
        }
      });
    }
  },
};
</script>
<style lang='scss' scoped>
#index {
  width: 100%;
  height: 100%;
  padding: 20px;
}


// ::v-deep .el-table .el-table__header-wrapper th,
// .el-table .el-table__fixed-header-wrapper th {
//   background-color: #fff;
// }
.level {
  display: flex;
  align-items: center;
  min-width: 504px;
}

.bor {
  display: inline-block;
  width: 50px;
  height: 1px;
  margin: 0 10px;
  background: #dfe6ec;
}
::v-deep .el-card{
    border-radius: 12px;
}

::v-deep .jv-button {
  &::before {
    content: "复制";
  }
}

::v-deep .el-table .row-expand-cover .cell .el-table__expand-icon {
  display: none;
}

::v-deep .el-table .el-table__expanded-cell {
  padding: 0;
}
</style>